package translate

import (
	"context"
	"encoding/json"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/gclient"
)

type LanguageObj struct {
	Data struct {
		Translations []struct {
			TranslatedText         string `json:"translatedText"`
			DetectedSourceLanguage string `json:"detectedSourceLanguage"`
		} `json:"translations"`
	} `json:"data"`
}

var (
	baseURL = "https://translation.googleapis.com/language/translate/v2"
	apiKey  = "AIzaSyATmTewyulgKtfNbKJFF5baIaFimyCX5Bs"
)

func TranslateText(ctx context.Context, text string, targetLang string) (languageText string, err error) {

	var (
		client      = g.Client()
		response    *gclient.Response
		languageObj LanguageObj
	)
	//client = client.Proxy("http://127.0.0.1:7890")
	if response, err = client.PostForm(ctx, baseURL, g.MapStrStr{
		"q":      text,
		"target": targetLang,
		"key":    apiKey,
	}); err != nil {
		return
	}
	defer response.Close()
	g.Log().Debug(ctx, response.Raw())

	if err = json.Unmarshal(response.ReadAll(), &languageObj); err != nil {
		return
	}
	languageText = languageObj.Data.Translations[0].TranslatedText
	return
}
